﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using myTwitter.BO;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Media.Animation;
using System.ComponentModel;
using myBoard.Framework;

namespace myTwitter.CutomControls
{
	/// <summary>
	/// Interaction logic for UsersNavigator.xaml
	/// </summary>
	public partial class UsersNavigator : UserControl
	{
        public delegate void UserInfoRequestedHandler(User user);
        public event UserInfoRequestedHandler UserInfoRequested;
        private int _maxNbOfDisplayerUsers=30;
        private int _currentPage=0;
        private Storyboard _loadingOnSB;
        private Storyboard _loadingOffSB;

		public UsersNavigator()
		{
			this.InitializeComponent();
            _loadingOnSB = this.Resources["LoadingOn"] as Storyboard;
            _loadingOffSB = this.Resources["LoadingOff"] as Storyboard;
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(UsersNavigator_DataContextChanged);
		}

        public void LoadingOn()
        {
            this.BeginStoryboard(_loadingOnSB);
            _preLoader.Start();
        }

        public void LoadingOff()
        {
            this.BeginStoryboard(_loadingOffSB);
            _preLoader.Stop();
        }

        void UsersNavigator_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            ObservableCollection<User> users = e.NewValue as ObservableCollection<User>;
            if (users != null && users.Count > 0)
            {
                ObservableCollection<User> filteredUsers = new ObservableCollection<User>();
                var f1 = users.Skip(_currentPage * _maxNbOfDisplayerUsers);
                var f2 = f1.Take(_maxNbOfDisplayerUsers);
                _usersLbx.ItemsSource = f2;
            }
        }

		private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            Button button = sender as Button;
            User user = button.DataContext as User;
            if (UserInfoRequested != null)
            {
                BackgroundWorker bw = new BackgroundWorker();
                bw.DoWork += (a, b) =>
                {
                    Relationship r = GlobalController.Instances.ITwitterProvider.GetRelationship(b.Argument as User);
                    b.Result = r;
                };
                bw.RunWorkerCompleted += (a, b) =>
                {
                    Mouse.OverrideCursor = null;
                    LoadingOff();
                    if (b.Error != null)
                    {
                        ExceptionsManager.LogException("Sinapse", b.Error, true);
                    }
                    else
                    {
                        user.Relationship = b.Result as Relationship;
                        UserInfoRequested(user);
                    }
                };
                bw.RunWorkerAsync(user);
                Mouse.OverrideCursor = Cursors.Wait;
                LoadingOn();
            }
		}

		private void _previousBt_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            _currentPage = Math.Max(0, _currentPage - 1);
            Navigate();
		}

		private void _nextBt_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            ObservableCollection<User> users = DataContext as ObservableCollection<User>;
            if(users!=null && users.Count>0)
            {
                _currentPage = Math.Min(users.Count/_maxNbOfDisplayerUsers, _currentPage + 1);
            }
            Navigate();
		}

        private void Navigate()
        {
            ObservableCollection<User> users = DataContext as ObservableCollection<User>;
            if (users != null && users.Count > 0)
            {
                ObservableCollection<User> filteredUsers = new ObservableCollection<User>();
                var f1 = users.Skip(_currentPage * _maxNbOfDisplayerUsers);
                var f2 = f1.Take(_maxNbOfDisplayerUsers);
                _usersLbx.ItemsSource = f2;
            }
        }
	}
}